package cn.ffcs.uec.recordsApproval.component;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;

import cn.ffcs.uec.recordsApproval.component.bean.RecordsHistoricFlowListBoxExtBean;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.recordsApproval.vo.HistoricFlow;
import cn.ffcs.uec.recordsApproval.vo.HistoricTaskVo;
import cn.ffcs.uec.recordsApproval.vo.TaskVo;

/**
 * 记录审批流转信息Ext
 * 
 * @author liuxiaodong
 *
 */
@Controller
@Scope("prototype")
public class RecordsHistoricFlowListBoxExt extends Div implements IdSpace {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2958205230504251930L;

	public static final String RECORDS_HISTORIC_FLOW_LISTBOX_EXT_ZUL = "/pages_nmg/recordsApproval/comp/recordsHistoricFlow_listbox_ext.zul";

	private RecordsHistoricFlowListBoxExtBean bean = new RecordsHistoricFlowListBoxExtBean();

	/**
	 * 选中的待办记录
	 */
	private TaskVo task;
	/**
	 * 选中的历史记录
	 */
	private HistoricTaskVo historicTask;
	
	/**
	 * 构造函数
	 */
	public RecordsHistoricFlowListBoxExt() {
		Executions.createComponents(RECORDS_HISTORIC_FLOW_LISTBOX_EXT_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(ApprovalConstants.ON_SELECTED_HISTORIC_FLOW, this,
				ApprovalConstants.ON_SELECTED_HISTORIC_FLOW_RESPONSE);

		this.addForward(
				ApprovalConstants.ON_SELECTED_OUTSTAFF_ENTRY_HISTORIC_FLOW, this,
				ApprovalConstants.ON_SELECTED_OUTSTAFF_ENTRY_HISTORIC_FLOW_RESPONSE);

		this.addForward(
				ApprovalConstants.ON_SELECTED_OUTSTAFF_CORRECT_HISTORIC_FLOW, this,
				ApprovalConstants.ON_SELECTED_OUTSTAFF_CORRECT_HISTORIC_FLOW_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {

	}

	public void onSelectHistoricOutStaffEntryFlowResponse(ForwardEvent event) {
		task = (TaskVo) event.getOrigin().getData();
		setModel(task);
	}

	public void onSelectHistoricOutStaffCorrectFlowResponse(ForwardEvent event) {

		task = (TaskVo) event.getOrigin().getData();
		setModel(task);

	}
	
	public void onSelectHistoricFlowResponse(ForwardEvent event) {
		
		task = (TaskVo) event.getOrigin().getData();
		setModel(task);
	}

	private void setModel(TaskVo taskVo) {
		if (null != taskVo) {
			String procInstId = taskVo.getProcInsId();
			List<HistoricFlow> historicFlows = ApprovalUtil
					.historicFlow(procInstId);
			ListModel dataList = new BindingListModelList(historicFlows, true);
			bean.getRecordsHistoricFlowListbox().setModel(dataList);
		}
	}
	
	/**
	 * 选中历史流程记录后的操作
	 * @param event
	 */
	public void onSelectHistoricResponse(ForwardEvent event) {
		historicTask = (HistoricTaskVo) event.getOrigin().getData();
		if (null != historicTask) {
			String procInstId = historicTask.getProcessInstanceId();
			List<HistoricFlow> historicFlows = ApprovalUtil
					.historicFlow(procInstId);
			ListModel dataList = new BindingListModelList(historicFlows, true);
			bean.getRecordsHistoricFlowListbox().setModel(dataList);
		}
	}

}
